﻿//-----------------------------------------------------------------------------------
// <copyright file="ReflectionExtensionsTest.cs" company="P.O.S Informatique">
//     Copyright (c) P.O.S Informatique. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------------------
namespace PosInformatique.Framework.Tests
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using PosInformatique.Framework;

    [TestClass]
    public class ReflectionExtensionsTest
    {
        [TestMethod]
        public void GetPrioritizedCustomAttributes_FromMethod_Test()
        {
            MethodInfo method;
            method = typeof(ClassWithMethodWithAttributes).GetMethod("Method");

            PrioritizedAttribute[] attributes;
            attributes = ReflectionExtensions.GetPrioritizedCustomAttributes<PrioritizedAttribute>(method).OrderBy(p => p.Value).ToArray();

            Assert.AreEqual(2, attributes.Length);
            Assert.AreEqual("Method1", attributes[0].Value);
            Assert.AreEqual("Method2", attributes[1].Value);
        }

        [TestMethod]
        public void GetPrioritizedCustomAttributes_FromClass_Test()
        {
            MethodInfo method;
            method = typeof(ClassWithAttributes).GetMethod("Method");

            PrioritizedAttribute[] attributes;
            attributes = ReflectionExtensions.GetPrioritizedCustomAttributes<PrioritizedAttribute>(method).OrderBy(p => p.Value).ToArray();

            Assert.AreEqual(2, attributes.Length);
            Assert.AreEqual("Class1", attributes[0].Value);
            Assert.AreEqual("Class2", attributes[1].Value);
        }

        [TestMethod]
        public void GetPrioritizedCustomAttributes_FromAssembly_Test()
        {
            MethodInfo method;
            method = typeof(ClassWithNoAttribute).GetMethod("Method");

            PrioritizedAttribute[] attributes;
            attributes = ReflectionExtensions.GetPrioritizedCustomAttributes<PrioritizedAttribute>(method).OrderBy(p => p.Value).ToArray();

            Assert.AreEqual(2, attributes.Length);
            Assert.AreEqual("Assembly1", attributes[0].Value);
            Assert.AreEqual("Assembly2", attributes[1].Value);
        }

        [AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
        public class PrioritizedAttribute : Attribute
        {
            public PrioritizedAttribute(string value)
            {
                this.Value = value;
            }

            public string Value
            {
                get;
                private set;
            }
        }

        [Prioritized("Class1")]
        [Prioritized("Class2")]
        private class ClassWithMethodWithAttributes
        {
            [Prioritized("Method1")]
            [Prioritized("Method2")]
            public void Method()
            {
            }
        }

        [Prioritized("Class1")]
        [Prioritized("Class2")]
        private class ClassWithAttributes
        {
            public void Method()
            {
            }
        }

        private class ClassWithNoAttribute
        {
            public void Method()
            {
            }
        }
    }
}
